<template>
    <!--  外框架-->
    <div class="framework1">
        <!--      内框架-->

        <div class="framework2" v-show="!clickTemplate">
            <!--        卷宗管理表格-->
            <div class="top-class-table">
                <div class="header">
                    <span>统计管理-></span>
                    <span>案件统计</span>
                </div>
                <!--          新增刷新-->
                <div class="button_group">
                    <!-- 单位选择 -->
                    <el-select v-if="loginUserInfo.responsibleScope === '5' || loginUserInfo.responsibleScope === '4'"
                        v-model="searchUnitId" placeholder="请选择所属部门" style="width: 250px;margin-left: 15px"
                        @change="reqDossierData" clearable>
                        <el-option v-for="item in unitData" :key="item.id" :label="item.name" :value="item.id" />
                    </el-select>
                    <!-- 导出按钮 -->
                    <el-button type="primary" size="mini" @click="exportData">导出</el-button>
                </div>
                <!--           数据表-->
                <div style="height: calc(100vh - 134px)">
                    <el-table :data="tableData" :row-style="{ height: '47px' }"
                        style="width: 100%;height:calc(100% - 68px)" :header-cell-style="{ background: '#f4f3f9' }">
                        <el-table-column prop="unitName" label="所属部门"></el-table-column>
                        <el-table-column prop="caseNo" label="案件编号">
                            <template v-slot="scope">
                                {{ `朔开（${scope.row.documentNumber}）检字[${scope.row.enterTime ? new
                                    Date(scope.row.enterTime).getFullYear() : ''}]第${scope.row.caseNo}号` }}
                            </template>
                        </el-table-column>
                        <el-table-column prop="caseName" label="案件名称"></el-table-column>
                        <el-table-column prop="process" label="案件状态">
                            <template v-slot="scope">
                                <span v-if="convertProcess(scope.row.process).flag">
                                    {{ convertProcess(scope.row.process).label }}
                                </span>
                                <el-popover v-else effect="light" trigger="hover" placement="top" width="auto">
                                    <template #default>
                                        <div>驳回说明: {{ scope.row.backReason }}</div>
                                    </template>
                                    <template #reference>
                                        <el-link :underline="false" type="danger">
                                            {{ convertProcess(scope.row.process).label }}
                                        </el-link>
                                    </template>
                                </el-popover>
                            </template>
                        </el-table-column>
                        <el-table-column prop="routineType" label="案件程序"></el-table-column>
                        <el-table-column prop="enterTime" label="录入时间"></el-table-column>
                        <el-table-column prop="transactorName" label="承办人"></el-table-column>
                    </el-table>
                    <!--    分页-->
                    <div class="footer">
                        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                            :page-sizes="[5, 10]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
                            :total="total">
                        </el-pagination>
                    </div>
                </div>
            </div>

        </div>
    </div>
</template>

<script>
import request from "@/utils/request";
import { serverIp } from "@/ipConfig/config";

export default {
    name: "Dossier",
    data() {
        return {
            serverIp: serverIp,
            //表格数据
            tableData: [],

            // 单位选择
            searchUnitId: '', // 选中的单位ID
            unitData: [], // 单位数据

            //表格分页
            currentPage: 1, //页码
            pageSize: 10, //页长
            total: 0,  //总表长

            //映射表
            caseTableStatus: [
                { key: '0', label: '待提交', flag: true },
                { key: '1', label: '科室待审核', flag: true },
                { key: '2', label: '科室审核未通过', flag: false },
                { key: '3', label: '法律待审核', flag: true },
                { key: '4', label: '法律审核未通过', flag: false },
                { key: '5', label: '部门待审核', flag: true },
                { key: '6', label: '部门审核未通过', flag: false },
                { key: '7', label: '法律待审核', flag: true },
                { key: '8', label: '法律审核未通过', flag: false },
                { key: '9', label: '领导待审批', flag: true },
                { key: '10', label: '领导审批未通过', flag: false },
                { key: '11', label: '已归档', flag: true },
            ], //案件表格状态

            // 登录用户信息
            loginUserInfo: {
                id: '',
                responsibleScope: '', //查询范围
                roleLevel: '', //角色等级
                unitId: '', //内设部门ID
                userRole: '', //角色ID
                permissions: [], // 按钮路径（数据库中的path）
            },
            // 按钮权限
            permissionIsShow: {
            }
        };
    },//data

    created() {
        const info = JSON.parse(localStorage.getItem("user"));
        this.loginUserInfo = {
            id: Number(info.id),
            responsibleScope: info.responsibleScope, // 查询范围
            roleLevel: Number(info.roleLevel),
            unitId: Number(info.unitId),
            userRole: Number(info.userRole),
            permissions: info.permissions.filter(item => Number(item.menuId) === 40).map(item => item.path),
        }
        this.permissionIsShow = {

        }
        this.load();
    },

    methods: {
        load() {
            this.getUnitData();
            this.getCaseData();
        },
        // 获取单位数据
        getUnitData() {
            if (this.loginUserInfo.unitId !== 1) {
                return
            } else {
                const responsibleScope = this.loginUserInfo.responsibleScope
                if (responsibleScope === '5') {
                    // 所有部门
                    request.get("/unit/filterAfterAllUnit").then(res => {
                        if (res.code === "200") {
                            this.unitData = res.data;
                        }
                    })
                } else if (responsibleScope === '4') {
                    // 部分部门
                    request.get("/unit/filterAfterAllUnit", {
                        params: {
                            userId: this.loginUserInfo.id,
                        }
                    }).then(res => {
                        if (res.code === "200") {
                            this.unitData = res.data;
                        }
                    })
                } else {
                    this.unitData = []
                }
            }
        },
        // 获取案件数据
        async getCaseData() {
            const responsibleScope = this.loginUserInfo.responsibleScope
            if (responsibleScope === '5') {
                // 查询所有案件
                await request.get("/case/statisticsFindCaseManageUnit", {
                    params: {
                        pageNum: this.currentPage,
                        pageSize: this.pageSize,
                    }
                }).then(res => {
                    if (res.code == '200') {
                        this.tableData = res.data.records;
                        this.total = res.data.total;
                    }
                })
            } else if (responsibleScope === '4') {
                // 查询自己负责单位的案件
                await request.get("/case/statisticsFindCaseManageUnit", {
                    params: {
                        pageNum: this.currentPage,
                        pageSize: this.pageSize,
                        userId: this.loginUserInfo.id,
                    }
                }).then(res => {
                    if (res.code == '200') {
                        this.tableData = res.data.records;
                        this.total = res.data.total;
                    }
                })
            } else {
                // 查询自己部门的所有案件
                await request.get("/case/statisticsFindCaseManageUnit", {
                    params: {
                        pageNum: this.currentPage,
                        pageSize: this.pageSize,
                        unitId: this.loginUserInfo.unitId,
                    }
                }).then(res => {
                    if (res.code == '200') {
                        this.tableData = res.data.records;
                        this.total = res.data.total;
                    }
                })
            }
        },
        // 导出数据
        exportData() {
            let params = [];
            // // 处理参数
            // if (this.search && this.search.trim() !== '') {
            //     params.push(`name=${encodeURIComponent(this.search)}`);
            // }
            // if (this.searchHomeShow === '1' || this.searchHomeShow === '0') {
            //     params.push(`homeShow=${encodeURIComponent(this.searchHomeShow)}`);
            // }

            const responsibleScope = this.loginUserInfo.responsibleScope
            if(responsibleScope === '5'){

            }else if (responsibleScope === '4') {
                params.push(`userId=${encodeURIComponent(this.loginUserInfo.id)}`);
            } else {
                params.push(`unitId=${encodeURIComponent(this.loginUserInfo.unitId)}`);
            }
            const url = `${this.serverIp}/case/exportCase${params.length ? '?' + params.join('&') : ''}`;
            // 不打开新窗口，直接下载
            console.log(url);
            window.location.href = url;
        },
        // 转换案件状态文本
        convertProcess(process) {
            const status = this.caseTableStatus.find(item => item.key === process) || { label: '未知状态', flag: true };
            return {
                label: status.label,
                flag: status.flag,
            };
        },

        //表尾分页
        handleSizeChange(val) {
            this.pageSize = val;
            this.getCaseData();
        },
        handleCurrentChange(val) {
            this.currentPage = val;
            this.getCaseData();
        },

    }//methods
}//default


</script>


<style scoped>
#roleSpan {
    color: black;
    text-decoration: none;
}


/*   外框架   */
.framework1 {
    height: calc(100vh - 88px);
    width: calc(100vh - 200px);
    margin-left: 8px;
    border: solid 1px #D3D3D3;
}

/*   文书内框架   */
.frameworkIn1 {
    height: calc(100vh - 92px);
    width: 100%;
    border: solid 1px #D3D3D3;
}

/*   内框架   */
.framework2 {
    height: calc(100vh - 92px);
    border: solid 1px #D3D3D3;
}

/*   文书标题   */
.templateHeader {
    font-size: 19px;
    font-weight: bold;
    padding: 4px 20px;
    height: 30px;
    border: 1px solid #CCCCCC;
    background-color: #E0ECFF;
}

/*   文书底边   */
.templateFooter {
    text-align: center;
    margin-top: -4px;
    padding: 4px 20px;
    height: 30px;
    border: 1px solid #CCCCCC;
    background-color: #E0ECFF;
}

/*   表格大标题   */
.header {
    font-size: 16px;
    font-weight: bold;
    padding: 4px 20px;
    height: 22px;
    background-color: #CCCCCC;
}

/*   表格按键组   */
.button_group {
    padding: 4px 10px;
    background-color: #DDDDDD;
}

/*   表格分页处   */
.footer {
    padding: 4px 10px;
    background-color: #f4f3f9;
}
</style>
